package main

import (
	"common/macaroon"
	"common/uuid"
	"encoding/json"
	"fmt"
	"github.com/spf13/cobra"
	"io/ioutil"
	"net/http"
)

var apikeyCmd = &cobra.Command{
	Use:   "apikey",
	Short: "apikey",
	RunE:  cmdApikey,
}

func cmdApikey(cmd *cobra.Command, args []string) error {
	email := "wx_50@qq.com"

	//creat a new project ,return project id, secret id
	type User struct {
		ID        uuid.UUID `json:"id"`
		SecretKey []byte    `json:"secret_key"`
	}
	var input = User{}
	//s, _ := json.Marshal(input)
	resp, err := http.Get("http://" + runConfig.AdminUrl + "/api/users/" + email)
	fmt.Println("runConfig.AdminUrl", runConfig.AdminUrl)
	if err == nil {
		data, _ := ioutil.ReadAll(resp.Body)
		err = json.Unmarshal(data, &input)
		fmt.Println(input.SecretKey)
		fmt.Println(input.ID)
		apikey, err := macaroon.NewAPIKey(input.ID, input.SecretKey)
		if err != nil {
			return err
		}
		fmt.Println(apikey.Serialize())
	}
	return nil
}
